package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.entity.AlarmInformationEntity;
import com.example.trackcircuitdiagnosis.service.AlarmInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController

@RequestMapping("/alarm-information")
@CrossOrigin
public class AlarmInformationController {


    @Autowired
    private AlarmInformationService alarmInformationService;

    // 新增或者更新
    @PostMapping
    @Operation(description = "保存警报详细信息")
    public boolean save(@RequestBody AlarmInformationEntity alarmInformation) {
        return alarmInformationService.saveOrUpdate(alarmInformation);
    }

    @DeleteMapping("/{id}")
    @Operation(
            description = "删除警报信息",
            parameters = {
                    @Parameter(name = "id", description = "要删除的警报信息id")
            }

    )
    public Boolean delete(@PathVariable Integer id) {
        return alarmInformationService.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除警报",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的警报信息")
            }

    )
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return alarmInformationService.removeByIds(ids);
    }

    @GetMapping
    @Operation(description = "查找全部")
    public List<AlarmInformationEntity> findAll() {
        return alarmInformationService.list();
    }

    @GetMapping("/{id}")
    @Operation(
            description = "查找警报信息",
            parameters = {
                    @Parameter(name = "id", description = "要查找警报信息id")
            }

    )
    public AlarmInformationEntity findOne(@PathVariable Integer id) {
        return alarmInformationService.getById(id);
    }

    @GetMapping("/page")
    @Operation(
            description = "分页查询",
            parameters = {
                    @Parameter(name = "pageNum", description = "要查找警报信息页数"),
                    @Parameter(name = "pageSize", description = "要查找警报信息每一页的数量")
            }

    )
    public Page<AlarmInformationEntity> findPage(@RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize) {
        QueryWrapper<AlarmInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return alarmInformationService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

}

